<template>
  <el-dialog
    :title="!dataForm.catIdentify ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="猫的编号" prop="catVaccineId">
      <el-input v-model="dataForm.catVaccineId" placeholder="猫的编号"></el-input>
    </el-form-item>
    <el-form-item label="第一针疫苗日期" prop="catVaccine1">
      <el-input v-model="dataForm.catVaccine1" placeholder="第一针疫苗日期"></el-input>
    </el-form-item>
    <el-form-item label="第二针疫苗日期" prop="catVaccine2">
      <el-input v-model="dataForm.catVaccine2" placeholder="第二针疫苗日期"></el-input>
    </el-form-item>
    <el-form-item label="第三针疫苗日期" prop="catVaccine3">
      <el-input v-model="dataForm.catVaccine3" placeholder="第三针疫苗日期"></el-input>
    </el-form-item>
    <el-form-item label="疫苗类型" prop="catVaccineType">
      <el-input v-model="dataForm.catVaccineType" placeholder="疫苗类型"></el-input>
    </el-form-item>
    <el-form-item label="疫苗接种地址" prop="catVaccineAddress">
      <el-input v-model="dataForm.catVaccineAddress" placeholder="疫苗接种地址"></el-input>
    </el-form-item>
    <el-form-item label="疫苗接种单位" prop="catVaccineUnit">
      <el-input v-model="dataForm.catVaccineUnit" placeholder="疫苗接种单位"></el-input>
    </el-form-item>
    <el-form-item label="负责人" prop="catVaccineCarer">
      <el-input v-model="dataForm.catVaccineCarer" placeholder="负责人"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          catIdentify: 0,
          catVaccineId: '',
          catVaccine1: '',
          catVaccine2: '',
          catVaccine3: '',
          catVaccineType: '',
          catVaccineAddress: '',
          catVaccineUnit: '',
          catVaccineCarer: ''
        },
        dataRule: {
          catVaccineId: [
            { required: true, message: '猫的编号不能为空', trigger: 'blur' }
          ],
          catVaccine1: [
            { required: true, message: '第一针疫苗日期不能为空', trigger: 'blur' }
          ],
          catVaccine2: [
            { required: true, message: '第二针疫苗日期不能为空', trigger: 'blur' }
          ],
          catVaccine3: [
            { required: true, message: '第三针疫苗日期不能为空', trigger: 'blur' }
          ],
          catVaccineType: [
            { required: true, message: '疫苗类型不能为空', trigger: 'blur' }
          ],
          catVaccineAddress: [
            { required: true, message: '疫苗接种地址不能为空', trigger: 'blur' }
          ],
          catVaccineUnit: [
            { required: true, message: '疫苗接种单位不能为空', trigger: 'blur' }
          ],
          catVaccineCarer: [
            { required: true, message: '负责人不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.catIdentify = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.catIdentify) {
            this.$http({
              url: this.$http.adornUrl(`/cat/vaccinerecord/info/${this.dataForm.catIdentify}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.catVaccineId = data.vaccineRecord.catVaccineId
                this.dataForm.catVaccine1 = data.vaccineRecord.catVaccine1
                this.dataForm.catVaccine2 = data.vaccineRecord.catVaccine2
                this.dataForm.catVaccine3 = data.vaccineRecord.catVaccine3
                this.dataForm.catVaccineType = data.vaccineRecord.catVaccineType
                this.dataForm.catVaccineAddress = data.vaccineRecord.catVaccineAddress
                this.dataForm.catVaccineUnit = data.vaccineRecord.catVaccineUnit
                this.dataForm.catVaccineCarer = data.vaccineRecord.catVaccineCarer
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/cat/vaccinerecord/${!this.dataForm.catIdentify ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'catIdentify': this.dataForm.catIdentify || undefined,
                'catVaccineId': this.dataForm.catVaccineId,
                'catVaccine1': this.dataForm.catVaccine1,
                'catVaccine2': this.dataForm.catVaccine2,
                'catVaccine3': this.dataForm.catVaccine3,
                'catVaccineType': this.dataForm.catVaccineType,
                'catVaccineAddress': this.dataForm.catVaccineAddress,
                'catVaccineUnit': this.dataForm.catVaccineUnit,
                'catVaccineCarer': this.dataForm.catVaccineCarer
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
